/* SVN FILE: $Id $ */
/**
 * Project Name : Smart Album
 * Copyright 2012 Sony Corporation
 *
 * @author         $Author $
 * @version        $Rev $
 * @lastrevision   $Date $
 * @filesource     $URL $
 * @requires FabricJS
 * @requires RaphaelJS
 */

function EbookEditor( config )
{

	var baseElement,
		canvases = new Object,
		pages = new Object,
		canvasWidth = '75%',
		canvasHeight = '800px';

	/**
	 * Constructor
	 */
	function init( options )
	{
		// Check for provided root Node
		if ( options.container !== undefined )
		{
			baseElement = options.container;
		}
		// Check dependencies
		if ( ! Raphael )
		{
			throw new Error('Raphael JS library required');
		}
		if ( ! fabric )
		{
			throw new Error('Fabric JS library required');
		}
	}

	/**
	 * Create an ebook editor page
	 */
	function createPage(page)
	{
		// Store the data
		pages[page.index] = page;
		// Create canvas and add object reference to collection
		var objCanvas = createCanvas(page.index);
		canvases[page.index] = {
			element: objCanvas,
			fCanvas: new fabric.Canvas(objCanvas.id)
		}
		// Add canvas to the page
		baseElement.appendChild(objCanvas);
		// Now set the height to a 4/3 dimension
		canvases[page.index].element.style.height = (getCanvasWidth(objCanvas) * (4/3)) + 'px';
		canvases[page.index].element.height = (getCanvasWidth(objCanvas) * (4/3));
		canvases[page.index].element.width = getCanvasWidth(objCanvas);
		// Now get the pixel height and width of rendered canvas element
		canvases[page.index].height = getCanvasHeight(objCanvas);
		canvases[page.index].width = getCanvasWidth(objCanvas);
		
		for ( var koma_num in page.komas )
		{
			createPageKoma(page.komas[koma_num], page.index);
		}
		//canvases[page.index].fCanvas.renderAll();
		// activate fabricJS
	}

	function createPageKoma( koma, idx )
	{
		var u = koma.u,
			v = koma.v,
			h = koma.h,
			w = koma.w;

		var koma_dimensions = {
			top: roundTo((parseFloat(v) * canvases[idx].height), 2),
			left: roundTo((parseFloat(u) * canvases[idx].width), 2),
			height: roundTo((parseFloat(h) * canvases[idx].height), 2),
			width: roundTo((parseFloat(w) * canvases[idx].width), 2)
		}

		//console.log({top: koma_dimensions.top, left: koma_dimensions.left, width: koma_dimensions.width, height: koma_dimensions.height, fill: '#fff', stroke: '#666'});


		/*
		canvases[idx].fCanvas.add(
			new fabric.Rect({ top: koma_dimensions.top, left: koma_dimensions.left, width: koma_dimensions.width, height: koma_dimensions.height, fill: '#fff', stroke: '#666' })
		);
		*/
			
		canvases[idx].element.getContext('2d').strokeStyle = '1px solid #000';
		canvases[idx].element.getContext('2d').strokeRect(koma_dimensions.left, koma_dimensions.top, koma_dimensions.width, koma_dimensions.height);

		var image = createImage(koma.images, koma.fileUri, koma_dimensions);
		console.log({
			img: image.element,
			sx: Math.abs(image.left * image.element.width),
			sy: Math.abs(image.top * image.element.height),
			sw: (image.width * koma_dimensions.width),
			sh: (image.height * koma_dimensions.height),
			dx: koma_dimensions.left,
			dy: koma_dimensions.top,
			dw: koma_dimensions.width,
			dh: koma_dimensions.height
		})


		canvases[idx].element.getContext('2d').drawImage(
			image.element,
			Math.abs(image.left * image.element.width),
			Math.abs(image.top * image.element.height),
			(image.width * koma_dimensions.width),
			(image.height * koma_dimensions.height),
			koma_dimensions.left,
			koma_dimensions.top,
			koma_dimensions.width,
			koma_dimensions.height
		);

	}


	function createImage(image, src, koma_dimensions)
	{
		var u = image.u,
			v = image.v,
			h = image.h,
			w = image.w;

		var newImage = {
			top: Math.abs(roundTo(parseFloat(v), 2)),
			left: Math.abs(roundTo(parseFloat(u), 2)),
			height: roundTo(parseFloat(h), 2),
			width: roundTo(parseFloat(w), 2)
		}

		newImage.element = document.createElement('img');
		newImage.element.src = src;

		//console.log('image:');
		//console.log(newImage);

		return newImage;

	}


	function getCanvasHeight(canvas)
	{
		return canvas.offsetHeight;
	}


	function getCanvasWidth(canvas)
	{
		return canvas.offsetWidth;
	}

	/**
	 * Create a canvas DOM element
	 */
	function createCanvas( id )
	{
		var newCanvas = document.createElement('canvas');
		newCanvas.id = 'canvas_' + id;
		newCanvas.style.width = canvasWidth;
		newCanvas.className = "ebook_page";
		return newCanvas;
	}

	/**
	 * Round / truncate a number to specified number of decimal places
	 */
	function roundTo( num, num_decimal_places )
	{
		return num.toFixed( num_decimal_places );
	}


	// Constructor call
	init( config );

	// Public Interface
	this.createPage = createPage;
	this.canvases = function(){ return canvases; }
}